#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

using System.Web;

using System.Collections;
using WaveMaster.Holidays.Club.Domain;
using WaveMaster.Holidays.Club.Service;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class TravelAgentForm : Form
    {
        TravelAgent m_travelAgent;
        User m_current_user;
        EditMode m_editmode;
        TravelAgentService m_travelAgentService;
        MemberTypeService m_memberTypeService;
        MainForm m_mainForm;
        UserControlManageTravelAgent m_parentForm;

        public TravelAgentForm(string name, TravelAgent user, User currentUser, MainForm mainForm, UserControlManageTravelAgent parentForm, EditMode editmode)
        {
            InitializeComponent();
            m_travelAgentService = (TravelAgentService)ServiceFactory.Instance().GetService(ServiceFactory.TRAVEL_AGENT_SERVICE);
            m_memberTypeService = (MemberTypeService)ServiceFactory.Instance().GetService(ServiceFactory.MEMBER_TYPE_SERVICE);
            m_current_user = currentUser;
            m_mainForm = mainForm;
            this.Name = name;
            m_parentForm = parentForm;
            initButtonEvent();
            initComboDataSource();
            if (user.TravelAgentID == 0)
            {
                clearForm();
            }
            else
            {
                m_travelAgent = m_travelAgentService.GetTravelAgentByID(user.TravelAgentID);
                loadUser(); 
                enableForm(editmode == EditMode.Edit);
            }
            setEditMode(editmode);
            //enableForm(false);
           
        }

        private void clearForm()
        {
            textBoxName.Text = string.Empty;
            textBoxUserCode.Text = "AUTONUMBER";// string.Empty;
            textBoxAddress.Text = string.Empty;
            textBoxZipCode.Text = string.Empty;
            textBoxContactPerson.Text = string.Empty;
            textBoxPhone.Text = string.Empty;
            textBoxFax.Text = string.Empty;
            textBoxEmail.Text = string.Empty;
            textBoxWebsite.Text = string.Empty;
            checkBoxActive.Checked = true;
            dateTimePickerExpiratedDate.Value = DateTime.Today.AddYears(1);
            dateTimePickerRegisterDate.Value = DateTime.Today;
            comboBoxMemberType.Text = string.Empty;

            textBoxRegNo.Text = string.Empty;
            textBoxFirstName.Text = string.Empty;
            textBoxLastName.Text = string.Empty;
            comboBoxGender.SelectedIndex = 0;
            textBoxMobileNo.Text = string.Empty;
            textBoxmemberEmail.Text = string.Empty;

            m_travelAgent = new TravelAgent();
            clearErrorProvider();
            enableForm(true);
            setEditMode(EditMode.Create);
            checkBoxActive.Focus();
        }

        private void initButtonEvent()
        {
            //toolBarButtonClear.Click += new EventHandler(toolBarButtonClear_Click);
            toolBarButtonSave.Click += new EventHandler(toolBarButtonSave_Click);
            //toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            //toolBarButtonSearch.Click += new EventHandler(toolBarButtonSearch_Click);
            toolBarButtonCancel.Click += new EventHandler(toolBarButtonCancel_Click);
        }
              
        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            setEditMode(EditMode.Edit);
            enableForm(!toolBarButtonEdit.Enabled);
        }

        void toolBarButtonCancel_Click(object sender, EventArgs e)
        {
            if (m_editmode == EditMode.Load)
                this.Close();
            else
            MessageBox.Show("Are you sure to cancel?", "Confirmation", MessageBoxButtons.YesNo,
               MessageBoxIcon.Question, new EventHandler(cancelConfirmation));
        }

        void cancelConfirmation(object sender, EventArgs e)
        {
             MessageBoxWindow msg = (MessageBoxWindow)sender;
             if (msg.DialogResult == DialogResult.Yes)
             {
                 this.Close();
             }
        }
        private void initComboDataSource()
        {
            comboBoxMemberType.DataSource = m_memberTypeService.LoadAllMemberType();

            IList gender = new ArrayList();
            gender.Add("Male");
            gender.Add("Female");
            comboBoxGender.DataSource = gender;
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            //loadAllUser();
            initComboDataSource();
            if (m_editmode == EditMode.Load || m_editmode == EditMode.Edit)
                loadUser();
        }

        private void clearErrorProvider()
        {
            errorProviderUser.SetError(textBoxUserCode, "");
            errorProviderUser.SetError(textBoxName, "");
            errorProviderUser.SetError(comboBoxMemberType, "");
        }
        private bool isValidForm()
        {
            clearErrorProvider();
            bool valid_a = textBoxUserCode.Text != string.Empty;
            bool valid_b = textBoxName.Text != string.Empty;
            bool valid_c = comboBoxMemberType.Text != string.Empty;
            if (!valid_a) errorProviderUser.SetError(textBoxUserCode, "User Code can not be empty");
            if (!valid_b) errorProviderUser.SetError(textBoxName, "User Name can not be empty");
            if (!valid_c) errorProviderUser.SetError(comboBoxMemberType, "Please choose member.");
            return valid_a && valid_b && valid_c;
        }

        void toolBarButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (isValidForm())
                {
                    m_travelAgent.Code = textBoxUserCode.Text;
                    m_travelAgent.Name = textBoxName.Text;
                    m_travelAgent.Active = checkBoxActive.Checked;
                    m_travelAgent.Address = textBoxAddress.Text;
                    m_travelAgent.ContactPerson = textBoxContactPerson.Text;
                    m_travelAgent.Email = textBoxEmail.Text;
                    m_travelAgent.Fax = textBoxFax.Text;
                    m_travelAgent.Phone = textBoxPhone.Text;
                    m_travelAgent.WebSite = textBoxWebsite.Text;
                    m_travelAgent.ZipCode = textBoxZipCode.Text;
                    m_travelAgent.RegisterDate = dateTimePickerRegisterDate.Value;
                    m_travelAgent.ExpiredDate = dateTimePickerExpiratedDate.Value;
                    m_travelAgent.MemberTypeEnum = MemberTypeEnum.Gold;// (MemberTypeEnum)Enum.Parse(typeof(MemberTypeEnum), comboBoxMemberType.Text);
                    m_travelAgent.MemberType = (MemberType)comboBoxMemberType.SelectedValue;
                    m_travelAgent.RegNo = textBoxRegNo.Text;
                    m_travelAgent.FirstName = textBoxFirstName.Text;
                    m_travelAgent.LastName = textBoxLastName.Text;
                    m_travelAgent.Gender = comboBoxGender.Text;
                    m_travelAgent.MobilePhone = textBoxMobileNo.Text;
                    m_travelAgent.MemberEmail = textBoxmemberEmail.Text;

                    if (m_travelAgent.TravelAgentID > 0)
                    {
                        m_travelAgent.ModificationBy = m_current_user.UserID;
                        m_travelAgentService.UpdateTravelAgent(m_travelAgent);
                    }
                    else
                    {
                        m_travelAgent.CreatenBy = m_current_user.UserID;
                        m_travelAgentService.InserTravelAgent(m_travelAgent);
                    }
                    m_parentForm.LoadAllTravelAgent();
                  //  MessageBox.Show("Record has been saved.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    MessageBox.Show("Record has been saved", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, new EventHandler(closeForm));
                    //clearForm();
                    //loadAllUser();
                    //setEditMode(EditMode.Saved);
                   // this.Close();
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

        }

        void closeForm(object sender, EventArgs e)
        {
            this.Close();
        }

        private void setEditMode(EditMode editMode)
        {
            toolBarButtonSave.Enabled = (editMode == EditMode.Create || editMode == EditMode.Edit || editMode == EditMode.Saved || editMode == EditMode.Delete)&& m_current_user.RoleDetails(this.Name).NewFlag;

            toolBarButtonEdit.Enabled = (editMode == EditMode.Load) && m_current_user.RoleDetails(this.Name).UpdateFlag;
           
            m_editmode = editMode;
            labelEditMode.Text = "[" + editMode + "]";
        }

        private void enableForm(bool enable)
        {
            textBoxName.ReadOnly = !enable;
            textBoxUserCode.ReadOnly = !enable;// ---autonumber
            checkBoxActive.Enabled = enable;
            textBoxAddress.ReadOnly = !enable;
            textBoxZipCode.ReadOnly = !enable;
            textBoxContactPerson.ReadOnly = !enable;
            textBoxPhone.ReadOnly = !enable;
            textBoxFax.ReadOnly = !enable;
            textBoxEmail.ReadOnly = !enable;
            textBoxWebsite.ReadOnly = !enable;
            dateTimePickerExpiratedDate.Enabled = enable;
            dateTimePickerRegisterDate.Enabled = enable;
            comboBoxMemberType.Enabled = enable;

            textBoxRegNo.ReadOnly = !enable;
            textBoxFirstName.ReadOnly = !enable;
            textBoxLastName.ReadOnly = !enable;
            comboBoxGender.Enabled = enable;
            textBoxMobileNo.ReadOnly = !enable;
            textBoxmemberEmail.ReadOnly = !enable;
        }

        private void loadUser()
        {
            m_travelAgent.MemberType = m_memberTypeService.GetMemberTypeByID(m_travelAgent.MemberType.m_member_type_id);

            textBoxName.Text = m_travelAgent.Name;
            textBoxUserCode.Text = m_travelAgent.Code;
            checkBoxActive.Checked = m_travelAgent.Active;
            textBoxAddress.Text = m_travelAgent.Address;
            textBoxZipCode.Text = m_travelAgent.ZipCode;
            textBoxContactPerson.Text = m_travelAgent.ContactPerson;
            textBoxPhone.Text = m_travelAgent.Phone;
            textBoxFax.Text = m_travelAgent.Fax;
            textBoxEmail.Text = m_travelAgent.Email;
            textBoxWebsite.Text = m_travelAgent.WebSite;
            dateTimePickerRegisterDate.Value = m_travelAgent.RegisterDate;
            dateTimePickerExpiratedDate.Value = m_travelAgent.ExpiredDate;
            comboBoxMemberType.Text = m_travelAgent.MemberType.ToString();

            textBoxRegNo.Text = m_travelAgent.RegNo;
            textBoxFirstName.Text = m_travelAgent.FirstName;
            textBoxLastName.Text = m_travelAgent.LastName;
            comboBoxGender.Text = m_travelAgent.Gender;
            textBoxMobileNo.Text = m_travelAgent.MobilePhone;
            textBoxmemberEmail.Text = m_travelAgent.MemberEmail;

        }
    }
}